import ToyCore from 'toy-core';
import JSON5 from 'json5';
import axios from 'axios';

const { setLocalConfig } = ToyCore.core.config;

let localConfig;

const initConfig = async () => {
  const url = './config/config.json';
  return axios.get(url);
};

// 获取public/config/config.json中的配置文件
export const getLocalConfig = async () => {
  if (!localConfig) {
    const response = await initConfig();
    localConfig = JSON5.parse(response.data);

    setLocalConfig(localConfig);
  }

  return localConfig;
};

// 可以在系统加载后再初始化的方法，节省系统初始加载时间
export const mountedInit = async () => {};

// 必须系统加载前初始化的方法
export const beforeCreateInit = async () => {
  // 获取配置文件
  await getLocalConfig();
};

export default { beforeCreateInit, mountedInit };
